package com.gexiaobao.pigeon.app.base

import android.app.Dialog
import android.app.ProgressDialog
import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Lifecycle
import androidx.recyclerview.widget.RecyclerView
import com.flyjingfish.openimagelib.OpenImage
import com.flyjingfish.openimagelib.enums.MediaType
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.ext.dismissLoadingExt
import com.gexiaobao.pigeon.app.ext.showLoadingExt
import com.gexiaobao.pigeon.app.soketback.BaseUiInterface
import com.gexiaobao.pigeon.app.util.LoadingUtil
import com.makeramen.roundedimageview.RoundedImageView
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import kotlinx.coroutines.Job
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.util.LogUtils

/**
 * 时间　: 2019/12/21
 * 作者　: hegaojian
 * 描述　: 你项目中的Activity基类，在这里实现显示弹窗，吐司，还有加入自己的需求操作 ，如果不想用Databind，请继承
 * BaseVmActivity例如
 * abstract class BaseActivity<VM : BaseViewModel> : BaseVmActivity<VM>() {
 */
abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDbActivity<VM, DB>(), BaseUiInterface {

    var mProgressDialog: ProgressDialog? = null
    var job: Job? = null
    abstract override fun initView(savedInstanceState: Bundle?)

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    /**
     * 有需要的地方设置沉浸式状态栏
     */
    open fun setStatusBarTranslucent() {
//        StatusBarUtil.setTranslucentForImageView(this, 0, null)
//        StatusBarUtil.setLightMode(this)
    }

    override fun showLoadingDialog(msg: String, cancelable: Boolean): Dialog {
        if (mProgressDialog != null && mProgressDialog!!.isShowing) {
            LogUtils.debugInfo("showLoadingDialog", "BaseBindingFragment show dialog bug?")
            mProgressDialog!!.cancel()
        }
        mProgressDialog = ProgressDialog.show(this, null, msg, true, cancelable)
        return mProgressDialog!!
    }

    override fun dismissLoadingDialog() {
        if (isFinishing || mProgressDialog == null || !mProgressDialog!!.isShowing) {
            return
        }
        mProgressDialog!!.cancel()
    }

    fun showLoadingAntiShake(text: String? = getString(R.string.loading)) {
        if (lifecycle.currentState == Lifecycle.State.STARTED) {
            LoadingUtil.show(this, text)
        }
    }

    fun dismissLoadingAntiShake() {
        LoadingUtil.dismiss(this)
    }

    fun openBigImage(view: AppCompatImageView, orgIcon: String) {
        OpenImage.with(this)
            .setClickImageView(view)
            .setSrcImageViewScaleType(ImageView.ScaleType.FIT_CENTER, true)
            .setImageUrl(orgIcon, MediaType.IMAGE).show()
    }

    fun openBigImage(view: RoundedImageView, orgIcon: String) {
        OpenImage.with(this)
            .setClickImageView(view)
            .setSrcImageViewScaleType(ImageView.ScaleType.CENTER_CROP, true)
            .setImageUrl(orgIcon, MediaType.IMAGE).show()
    }

    fun openAdapterBigImage(position: Int, view: RecyclerView, data: MutableList<String>) {
        OpenImage.with(this)
            .setClickRecyclerView(view) { _, _ -> R.id.ivItemProofPicture }
            /** 点击的ImageView的ScaleType类型（如果设置不对，打开的动画效果将是错误的） */
            .setSrcImageViewScaleType(ImageView.ScaleType.CENTER_CROP, true)
            .setImageUrlList(data, MediaType.IMAGE)
            .setClickPosition(position)
            .show()
    }

    fun openAdapterBigImage(position: Int, view: SwipeRecyclerView, data: MutableList<String>) {
        OpenImage.with(this)
            .setClickRecyclerView(view) { _, _ -> R.id.ivItemImageView }
            /** 点击的ImageView的ScaleType类型（如果设置不对，打开的动画效果将是错误的） */
            .setSrcImageViewScaleType(ImageView.ScaleType.CENTER_CROP, true)
            .setImageUrlList(data, MediaType.IMAGE)
            .setClickPosition(position)
            .show()
    }

    /**
     * 检查是否为整数
     */
    fun isInteger(str: String): Boolean {
        return str.toIntOrNull() != null
    }

    override fun onDestroy() {
        super.onDestroy()
        if (job != null) {
            job?.cancel()
            job = null
        }
    }

    /* */
    /**
     * 在任何情况下本来适配正常的布局突然出现适配失效，适配异常等问题，只要重写 Activity 的 getResources() 方法
     *//*
    override fun getResources(): Resources {
        AutoSizeCompat.autoConvertDensityOfGlobal(super.getResources())
        return super.getResources()
    }*/
}